/****************************************************************************/
/*
* test sample for xml API.
*/
/****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cms_common.h"
#include "cms_log.h"
#include "libmdm.h"
#include "cms_msg.h"
#include "cms_mdm.h"
#include "cms_event.h"
#include "cmsXml_object.h"


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#include <libmdm.h>
#include <cmsXml_object.h>
//#include <cmsTask_common_lib.h>
#include "cms_msg.h"
#include "cms_mdm.h"
#include "cms_common.h"
#include "cms_event.h"
#include "cms_log.h"
#include <sys/socket.h>
#include <sys/un.h>
#include "cms_serviceCmdCode.h"




int main(){
	cmsXmlCb_t *xmlShmCb= NULL;
	char objPathName[256] = {0};
	void *object;
	cmsoffset_t objOff;
	
	//setParaValue_t *setPtr, *tmpPtr;
	uintptr_t *parameter;
	uint32_t u32;
	char **temp;
	parameterValue_t *value, *ptr;
	uint32_t  type_size = 0;//, type_size1 = 0, type_size2 = 0, type_size3 = 0, attr = 0;
	int count = 0, i = 0;
	int ret = -1;
	char c;

	int size = sizeof(long);

	
	printf("xmlTest usage:\n");
	printf("xml api test!!, sizeof long:%d\n\r\n", size);
	printf("0-- init xml share memory.\n");
	printf("g-- get parameter value.\n");
	printf("t/p--  set parameter value.\n");
	printf("a-- add object.\n");   /*forwarding object*/
	printf("d-- del object.\n");	/*del forwarding object*/
	printf("c/b/e/f -- send notify");
	printf("s-- save xml.\n");
	printf("x-- deinit,exit test process.\n");
	printf("please input num to test:\n");
	printf("\r\n\r\n\r\n");




	cms_registerModule(CMS_SAMPLE);

	set_log_level(CMS_SAMPLE, LL_ERROR);

	while((c = getchar())){
		if (c == '0'){//init xml share memory
		
			xmlShmCb = XMLSHMINIT();
			if (xmlShmCb ==NULL){
				printf("xmlShm init error.\r\n");
			}else{
				printf("xmlShm init success.\r\n");
			}
		}	
		else if (c == 'g'){ /*get xml all object*/

			if(xmlShmCb == NULL){
				printf("xmlShm is NULL, please init first.\n");
				continue;
			}
			
			/*get device info*/
			object = cmsXmlGetObjectByOID(xmlShmCb, OID_InternetGatewayDevice_DeviceInfo, 0);
			cmsObj_InternetGatewayDevice_DeviceInfo_t *deviceInfo = (cmsObj_InternetGatewayDevice_DeviceInfo_t *)object;
			if(deviceInfo)
				CMS_LOG(LL_INFO, "device provisionCode %s.\n", deviceInfo->ProvisioningCode);

			/*get ntp server info*/
			object = cmsXmlGetObjectByOID(xmlShmCb, OID_InternetGatewayDevice_Time, 0);
			cmsObj_InternetGatewayDevice_Time_t *time = (cmsObj_InternetGatewayDevice_Time_t *)object;
			if(time)
				CMS_LOG(LL_INFO, "NTP server status %s, NTPServer1: %s,NTPServer2: %s,NTPServer3: %s,NTPServer4: %s,NTPServer5: %s,TimeZone: %s.\n", 
					((time->Enable  == 0) ? "off": "on"), 
					time->NTPServer1, time->NTPServer2,time->NTPServer3,
					time->NTPServer4,time->NTPServer5,time->LocalTimeZone);

			/*get lan device info*/
			int lanMax = InternetGatewayDevice_LANDevice_i_LANEthernetInterfaceConfig_i_Max;
			for(i = 0; i < lanMax; i++){
				object = cmsXmlGetObjectByOID(xmlShmCb, OID_InternetGatewayDevice_LANDevice_i_LANEthernetInterfaceConfig_i, cmsXmlConstructIAValue(2,"1,%d", i+1) );
				cmsObj_InternetGatewayDevice_LANDevice_i_LANEthernetInterfaceConfig_i_t *lan = (cmsObj_InternetGatewayDevice_LANDevice_i_LANEthernetInterfaceConfig_i_t *)object;
				if(lan != NULL)
					CMS_LOG(LL_INFO, "lan %d device status %s.\r\n",  (i+1), ((lan->Enable  == 0) ? "off" : "on"));
			}
			

			/*get account info*/
			object = cmsXmlGetObjectByOID(xmlShmCb, OID_InternetGatewayDevice_X_GC_EXT_X_GC_LOGIIN_i, cmsXmlConstructIAValue(1,"%d",1));
			cmsObj_InternetGatewayDevice_X_GC_EXT_X_GC_LOGIIN_i_t *login1 = (cmsObj_InternetGatewayDevice_X_GC_EXT_X_GC_LOGIIN_i_t *)object;
			if(login1)
				CMS_LOG(LL_INFO, "account 1 userlever: %s, username: %s,passwd: %s.\n", login1->UserLevel, login1->Username, login1->Passwd);

	
			sprintf(objPathName, "InternetGatewayDevice.X_GC_EXT.X_GC_LOGIIN.%d", 2);
			cmsObj_InternetGatewayDevice_X_GC_EXT_X_GC_LOGIIN_i_t *login2 = (cmsObj_InternetGatewayDevice_X_GC_EXT_X_GC_LOGIIN_i_t *)cmsXmlGetObjectByName(xmlShmCb, objPathName);
			if(login2 != NULL)
				CMS_LOG(LL_INFO, "account 2 userlever: %s, username: %s,passwd: %s.\n", login2->UserLevel, login2->Username, login2->Passwd);

	
			/*Layer3Forwarding info*/
			int max = InternetGatewayDevice_Layer3Forwarding_Forwarding_i_Max;
			for(i = 0; i < max; i++){
				object = cmsXmlGetObjectByOID(xmlShmCb, OID_InternetGatewayDevice_Layer3Forwarding_Forwarding_i, cmsXmlConstructIAValue(1,"%d", i+1));
				cmsObj_InternetGatewayDevice_Layer3Forwarding_Forwarding_i_t *forward = (cmsObj_InternetGatewayDevice_Layer3Forwarding_Forwarding_i_t *)object;
				if(forward != NULL){
					CMS_LOG(LL_INFO, "forward  rule%d,  enable status: %d, MTU: %d, sourceIP: %s, dstIP: %s, gwIP: %s.\n",
						(i+1),forward->Enable, forward->MTU, forward->SourceIPAddress, forward->DestIPAddress, forward->GatewayIPAddress);
				}
			}


			/**********get parameter with another way*************/
			/*!!!NOTE, parameter type must defined as uintptr_t to compatible with 64&32 system.*/
			/**************(1)*************/
			char *p = "NTPServer1";
			count = 1;
			parameter = malloc(sizeof(void*)*1);
			temp = (char **)parameter;
			*parameter = (uintptr_t)p;
			object = cmsXmlGetObjectByOID(xmlShmCb, OID_InternetGatewayDevice_Time, 0);
			objOff = cmsXmlGetPtrOffset(xmlShmCb, object);
			printf("before get parameter.\n");
			value = cmsXmlGetParameter(xmlShmCb, objOff, count, temp);
			ptr = value;
			for (i = 0; i < count; i++){
				printf("type_size:%x\r\n", ptr->type_size);

				if ((ptr->type_size & PARAMETER_TYPE_STRING) == PARAMETER_TYPE_STRING){
					printf("string:%s\r\n", (char *)ptr->value);

				}
				else{
					type_size = ptr->type_size & ~PARAMETER_TYPE_CLASS_BITS;
					memcpy(&u32, ptr->value, type_size);
					printf("value:%u\r\n", u32);

					printf("max:%u, min:%u\r\n", ptr->max.unsign.u32, ptr->min.unsign.u32);
				}
				ptr++;
			}
			free(temp);
			cmsXml_freeParameterBuffer(value, count); /*must free after get parameter*/

			/**************(2)*************/
			char paraValue[16] = {0};
			cmsXmlGetOneParameterByOID(xmlShmCb, OID_InternetGatewayDevice_Time, 0, "NTPServer1", paraValue);
			printf("ntpServer1: %s\n", paraValue);

			cmsXmlSetOneParameterByOID(xmlShmCb, OID_InternetGatewayDevice_Time, 0, "NTPServer1", PARAMETER_TYPE_STRING, "11testServ");

			memset(paraValue, 0, sizeof(paraValue));
			cmsXmlGetOneParameterByOID(xmlShmCb, OID_InternetGatewayDevice_Time, 0, "NTPServer1", paraValue);
			printf("ntpServer1: %s\n", paraValue);


			cmsXmlSetOneParameterByObjPath(xmlShmCb, "InternetGatewayDevice.Time", "NTPServer1", PARAMETER_TYPE_STRING, "22testServ");
			memset(paraValue, 0, sizeof(paraValue));
			cmsXmlGetOneParameterByObjPath(xmlShmCb, "InternetGatewayDevice.Time", "NTPServer1", paraValue);
			printf("ntpServer1: %s\n", paraValue);

			
			
			/**************(3)*************/
			uint16_t vlanid = 0;
			uint32_t oid = OID_InternetGatewayDevice_WANDevice_i_WANConnectionDevice_i_X_CT_COM_WANEponLinkConfig;
			uint64_t ia = cmsXmlConstructIAValue(2,"1,%d", 1);
			cmsXmlGetOneParameterByOID(xmlShmCb, oid, ia, "VLANIDMark", &vlanid);
			printf("vlanid is %d\n", vlanid);

			vlanid = 11;
			cmsXmlSetOneParameterByOID(xmlShmCb, oid, ia, "VLANIDMark", PARAMETER_TYPE_UINT16, &vlanid);	


			/*tr069 get parameter full path sample*/
			#if 0
			cwmp_getObjParaList_t *paraList = NULL;
			cwmp_getObjParaList_t *curPara = NULL;
			cmsXmlGetObjParaList(xmlShmCb, "InternetGatewayDevice.", true, &paraList);
			curPara = paraList;
			i = 0;
			while(curPara){
				printf("tr069 obj para name : %s, writeable : %s\n", curPara->parameterPath, ((curPara->writeable == true) ? "true" : "false"));
				curPara = curPara->next;
			}
			
			cmsXmlFreeObjParaList(paraList);
			#endif

			/*tr069 set attr*/
			#if 1
			cmsXmlSetParameterNotifyAttr(xmlShmCb,"InternetGatewayDevice.Time.",1);

			cwmp_getParaAttrList_t *attrList = NULL, *curAttr = NULL;
			cmsXmlGetParametereNotifyAttr(xmlShmCb,"InternetGatewayDevice.Time.", &attrList);
			curAttr = attrList;
			i = 0;
			while(curAttr){
				printf("1tr069 obj para name : %s, notifyAttr : %d\n", curAttr->parameterName, curAttr->notfiyType);
				curAttr = curAttr->next;
			}
			cmsXmlFreeNotifyAttrList(attrList);

			cmsXmlSetParameterNotifyAttr(xmlShmCb,"InternetGatewayDevice.Time.",2);

			cmsXmlGetParametereNotifyAttr(xmlShmCb,"InternetGatewayDevice.Time.", &attrList);
			curAttr = NULL;
			curAttr = attrList;
			i = 0;
			while(curAttr){
				printf("2tr069 obj para name : %s, notifyAttr : %d\n", curAttr->parameterName, curAttr->notfiyType);
				curAttr = curAttr->next;
			}
			cmsXmlFreeNotifyAttrList(attrList);

			#endif
	
		}
		else if(c == 'a'){/*add object*/
			if(xmlShmCb == NULL){
				printf("xmlShm is NULL, please init first.\n");
				continue;
			}

			/*add obj by path*/

			uint8_t enable = 1;
			paraValueSettingList_t *paralist = newParameterList();
			setParameterToList(paralist, "SourceIPAddress", PARAMETER_TYPE_STRING, &enable);
			setParameterToList(paralist, "SourceIPAddress", PARAMETER_TYPE_STRING, "192.168.1.88");
			setParameterToList(paralist, "DestIPAddress", PARAMETER_TYPE_STRING, "192.168.1.99");
			setParameterToList(paralist, "GatewayIPAddress", PARAMETER_TYPE_STRING, "192.168.1.66");
			cmsXmlAddObjByOIDWithParaList(xmlShmCb, OID_InternetGatewayDevice_Layer3Forwarding_Forwarding_i,cmsXmlConstructIAValue(1,"%d",3), paralist);
			freeParameterList(paralist);

	
		}
		else if(c == 'd')/*delete object*/{
			if(xmlShmCb == NULL){
				printf("xmlShm is NULL, please init first.\n");
				continue;
			}

			/*del by obj name*/
			char ObjNameStr[128] = {0};

			snprintf(ObjNameStr, sizeof(ObjNameStr), "InternetGatewayDevice.Layer3Forwarding.Forwarding.%d",3);
			cmsXmlDelObjByPathName(xmlShmCb, ObjNameStr);
		}
		else if(c == 't'){/*set parameter*/
			if(xmlShmCb == NULL){
				printf("xmlShm is NULL, please init first.\n");
				continue;
			}
			#if 1
			object = cmsXmlGetObjectByOID(xmlShmCb, OID_InternetGatewayDevice_Layer3Forwarding_Forwarding_i,cmsXmlConstructIAValue(1,"%d",3));
			if(object){

				objOff = cmsXmlGetPtrOffset(xmlShmCb, object);

				paraValueSettingList_t *paralist = newParameterList();
			
				uint8_t Enable = 0;
				setParameterToList(paralist, "SourceIPAddress", PARAMETER_TYPE_STRING, "192.168.1.11");
				setParameterToList(paralist, "Enable", PARAMETER_TYPE_BOOLEAN, &Enable);
				setParameterToList(paralist, "DestIPAddress", PARAMETER_TYPE_STRING, "7.7.7.7");
				setParameterToList(paralist, "GatewayIPAddress", PARAMETER_TYPE_STRING, "8.8.8.8");
				cmsXmlSetParameterByParaList(xmlShmCb,objOff,paralist);
				freeParameterList(paralist);

			#ifdef CWMP
				paraValueSettingList_t *paralist = newParameterList();
				setParameterToList(paralist, "SourceIPAddress", PARAMETER_TYPE_STRING, "192.168.1.11");
				cmsXmlCwmpSetParam(xmlShmCb,objOff,paralist, TRUE);
				freeParameterList(paralist);
			#endif
			}
			
			#else
			cms_multiObjSettingList_t *pMultiObjList = newMultiObjList();
			addObjToListByName(pMultiObjList, "InternetGatewayDevice.Test", OBJ_SET);
			setParamToMultiObjList(pMultiObjList, "para1", PARAMETER_TYPE_STRING, "test1");
			setParamToMultiObjList(pMultiObjList, "para2", PARAMETER_TYPE_STRING, "test2");
			addObjToListByName(pMultiObjList, "InternetGatewayDevice.Test1", OBJ_SET);
			setParamToMultiObjList(pMultiObjList, "para1", PARAMETER_TYPE_STRING, "test3");
			setParamToMultiObjList(pMultiObjList, "para2", PARAMETER_TYPE_STRING, "test4");
			addObjToListByName(pMultiObjList, "InternetGatewayDevice.Test2.1", OBJ_ADD);
			setParamToMultiObjList(pMultiObjList, "para1", PARAMETER_TYPE_STRING, "testString5");
			setParamToMultiObjList(pMultiObjList, "para2", PARAMETER_TYPE_STRING, "testString6");
			addObjToListByOID(pMultiObjList, OID_InternetGatewayDevice_Test2_i, cmsXmlConstructIAValue(1,"%d",2), OBJ_ADD);
			setParamToMultiObjList(pMultiObjList, "para1", PARAMETER_TYPE_STRING, "testString7");
			setParamToMultiObjList(pMultiObjList, "para2", PARAMETER_TYPE_STRING, "testString8");
			addObjToListByOID(pMultiObjList, OID_InternetGatewayDevice_Test2_i, cmsXmlConstructIAValue(1,"%d",3), OBJ_DEL);
			addObjToListByName(pMultiObjList, "InternetGatewayDevice.Test2.4", OBJ_ADD);
			cmsXml_setMultiObj(xmlShmCb, pMultiObjList, TRUE, TRUE);
			freeMultiObjList(pMultiObjList);
			/*
			cms_multiObjSettingList_t *pCurObj = pMultiObjList; 
			paraValueSettingList_t *pCurParamList = NULL;
			while(pCurObj){
				if(pCurObj->objectInfo.isPathName == TRUE)
					printf("obj path: %s, action:%s\n", 
						pCurObj->objectInfo.object.pathName, 
						((pCurObj->type == OBJ_ADD) ? "add object" : ((pCurObj->type == OBJ_DEL) ? "del object" : "set object")));
				else
					printf("obj OID: %d, 0x%16x, action:%s\n", 
						pCurObj->objectInfo.object.objOID.OID, pCurObj->objectInfo.object.objOID.OIA, 
						((pCurObj->type == OBJ_ADD) ? "add object" : ((pCurObj->type == OBJ_DEL) ? "del object" : "set object")));

				pCurParamList = pCurObj->paramList;
				while(pCurParamList){
					printf("paraname %s, value %s\n", pCurParamList->parameter, pCurParamList->value);
					pCurParamList = pCurParamList->next;
				}
				
				pCurObj = pCurObj->next;
			}*/
			
			#endif

			
			printf("cmsXmlSetParameter end!!\r\n");
		}
		else if(c == 'p'){/*set parameter*/
			if(xmlShmCb == NULL){
				printf("xmlShm is NULL, please init first.\n");
				continue;
			}
			object = cmsXmlGetObjectByOID(xmlShmCb, OID_InternetGatewayDevice_Layer3Forwarding_Forwarding_i,cmsXmlConstructIAValue(1,"%d",1));
			if(object){

				objOff = cmsXmlGetPtrOffset(xmlShmCb, object);
				paraValueSettingList_t *paralist = newParameterList();
				
				uint8_t Enable = 1;
				setParameterToList(paralist, "SourceIPAddress", PARAMETER_TYPE_STRING, "192.168.1.100");
				setParameterToList(paralist, "Enable", PARAMETER_TYPE_BOOLEAN, &Enable);
				setParameterToList(paralist, "DestIPAddress", PARAMETER_TYPE_STRING, "6.6.6.6");
				setParameterToList(paralist, "GatewayIPAddress", PARAMETER_TYPE_STRING, "9.9.9.9");
				cmsXmlSetParameterByParaList(xmlShmCb,objOff,paralist);
				freeParameterList(paralist);
			}
			printf("cmsXmlSetParameter end!!\r\n");
		}
		else if(c == 'c'){/*eventy notify sample*/
			if(xmlShmCb == NULL){
				printf("xmlShm is NULL, please init first.\n");
				continue;
			}
			
			uint8_t content = 100;
			ret = cms_eventNotify(CMS_EVENT_SAMPLE_1, PARAMETER_TYPE_UINT8, &content);
			if(ret == -1){
				printf("cms event notify error.\n");
			}
		}
		else if(c == 'b'){/*eventy notify sample*/
			if(xmlShmCb == NULL){
				printf("xmlShm is NULL, please init first.\n");
				continue;
			}
			
			uint16_t content = 500;
			ret = cms_eventNotify(CMS_EVENT_SAMPLE_2, PARAMETER_TYPE_UINT16, &content);
			if(ret == -1){
				printf("cms event notify error.\n");
			}

		}else if(c == 'e'){/*eventy notify sample*/
			if(xmlShmCb == NULL){
				printf("xmlShm is NULL, please init first.\n");
				continue;
			}
			
			uint32_t content = 999999;
			ret = cms_eventNotify(CMS_EVENT_SAMPLE_3, PARAMETER_TYPE_UINT32, &content);
			if(ret == -1){
				printf("cms event notify error.\n");
			}
			
		}else if(c == 'f'){/*eventy notify sample*/
			if(xmlShmCb == NULL){
				printf("xmlShm is NULL, please init first.\n");
				continue;
			} 

			
			ret = cms_eventNotify(CMS_EVENT_SAMPLE_4, PARAMETER_TYPE_STRING, "test-string");
			if(ret == -1){
				printf("cms event notify error.\n");
			}
		}
		else if (c == 's'){ /*save xml*/
			if(xmlShmCb == NULL){
				printf("xmlShm is NULL, please init first.\n");
				continue;
			}
			
			ret = cmsXmlSave(xmlShmCb);
			if(ret == 0)
				printf("save xml success.\n");
			else 
				printf("save xml fail.\n");
		
		}
		else if (c == 'x'){ /*deinit share memory*/
			if(xmlShmCb == NULL){
				printf("xmlShm is NULL, please init first.\n");
				continue;
			}
			
			XMLSHMDEINIT(xmlShmCb);

			break;
		}else if(c == 'r'){
			cmsXmlUpgradeConf("/bin/config.xml");/*define the xml*/
		}else if(c == 'u'){
			cmsXmlRestoreDefault(RESTORE_SHORT_NONE);
		}
		
	}
	cms_unregisterModule(CMS_SAMPLE); /* put it here since cms_eventNotify is non-block call, should not un_register it.*/

	printf("xmlTest Exit.\n");
	return 0;
}

#if 0  //object notify dbus sample 
/****************************************************************************/
/*
* dbus sample.
*/
/****************************************************************************/
#include "libmdm.h"
#include "cms_msg.h"
#include "cms_event.h"
#include "cms_log.h"
#include "cmsXml_object.h"

cmsXmlCb_t *xmlShmCb_g = NULL;

/* this function run as a Thread */
static void *cmsSample_threadHandler(void *arg){
	cmsEvent_t *eventRecv = NULL;
	cms_objDbusNotify_t *dbusNotify = NULL;
	cms_paramChgList_t *pCurParam = NULL;
	void *object = NULL;
	
	while(1){
		if(cms_eventRecv(&eventRecv) != CMS_NOK){
			/* cms object-changed event */
			if(eventRecv->notify.eventNotify.eventId == CMS_EVENT_OBJ_NOTIFY_DBUS){
				printf("event content %s\n", eventRecv->notify.eventNotify.eventContent);
				dbusNotify = cmsXml_getChgObjInfo(eventRecv->notify.eventNotify.eventContent);
				printf("oid %d, oia %#llx, action %d\n", dbusNotify->objID, dbusNotify->objIA, dbusNotify->objAction);

				pCurParam = dbusNotify->parameterList;
				while(pCurParam){
					printf("changed parameter %s\n", pCurParam->paramName);
					pCurParam = pCurParam->next;
				}

				
				if(dbusNotify->objID == OID_InternetGatewayDevice_X_GC_EXT_X_GC_LOGIIN_i)
				{
					cmsObj_InternetGatewayDevice_X_GC_EXT_X_GC_LOGIIN_i_t *loginObj = NULL;
					
					
					
					if(dbusNotify->objAction == CMS_OBJ_ADD)
					{
						printf("Oid %d oia %#llx, ADD object\n", dbusNotify->objID, dbusNotify->objIA);
					}
					else if(dbusNotify->objAction == CMS_OBJ_DEL)
					{
						printf("Oid %d oia %#llx, DEL object\n", dbusNotify->objID, dbusNotify->objIA);
					}
					else if(dbusNotify->objAction == CMS_OBJ_SET)
					{
						printf("Oid %d oia %#llx, SET object\n", dbusNotify->objID, dbusNotify->objIA);
						
						object = cmsXmlGetObjectByOID(xmlShmCb_g, dbusNotify->objID, dbusNotify->objIA);
						if(object == NULL){
							printf("get object NULL !!!");
							continue;
						}
						
						loginObj = (cmsObj_InternetGatewayDevice_X_GC_EXT_X_GC_LOGIIN_i_t *)object;
					
						if(cmsXml_checkParamChg(dbusNotify, "Username") == TRUE)
						{
							printf("parameter: Username changed, new value:%s\n", loginObj->Username);
						}
						else if(cmsXml_checkParamChg(dbusNotify, "Passwd") == TRUE)
						{
							printf("parameter: Passwd changed, new value:%s\n", loginObj->Passwd);
						}
					}
				}
				else if(dbusNotify->objID == OID_InternetGatewayDevice_LANDevice_i)
				{
					// check this object
				}
				
				
				cmsXml_freeChgObjInfo(&dbusNotify);
			}
			/* cms event other than cms-object-change */
			else if(eventRecv->notify.eventNotify.eventId == CMS_EVENT_LAN_LINK_UP){
				printf("lan port up\n");
			}else if(eventRecv->notify.eventNotify.eventId == CMS_EVENT_LAN_LINK_DOWN){
				printf("lan port down\n");
			}else if(eventRecv->notify.eventNotify.eventId == CMS_EVENT_WAN_LINK_UP){
				printf("wan port up\n");
			}else if(eventRecv->notify.eventNotify.eventId == CMS_EVENT_WAN_LINK_DOWN){
				printf("wan port down\n");
			}else 
				CMS_LOG(LL_ERROR, "unSubscribe event id %d.\n", eventRecv->notify.eventNotify.eventId);

			free(eventRecv);
			eventRecv = NULL;
		}
	}

	return NULL;
}


int main(){
	
	xmlShmCb_g = XMLSHMINIT();
	if (xmlShmCb_g ==NULL){
		printf("xmlShm init error.\r\n");
	}else{
		printf("xmlShm init success.\r\n");
	}
	
	cms_registerModule(CMS_SAMPLE);

	set_log_level(CMS_SAMPLE, LL_ERROR);

	/*subscibe OID*/
	const uint32_t object_node[] = {
		OID_InternetGatewayDevice,
		OID_InternetGatewayDevice_LANDevice_i,
		OID_InternetGatewayDevice_DeviceInfo_X_CT_COM_Alarm_AlarmConfig_i,
		OID_InternetGatewayDevice_Services_VoiceService_i_PhyInterface_i,
		OID_InternetGatewayDevice_X_GC_EXT_X_GC_LOGIIN_i,
		OID_InternetGatewayDevice_UploadDiagnostics
	};

	/*subscibe event*/
	const uint32_t event[] = {
		CMS_EVENT_OBJ_NOTIFY_DBUS, //this must exist if subscribe cms-object-changed event
		CMS_EVENT_LAN_LINK_UP,
		CMS_EVENT_LAN_LINK_DOWN,
		CMS_EVENT_WAN_LINK_UP,
		CMS_EVENT_WAN_LINK_DOWN
	};

	if(cmsXml_subcribeObject(sizeof(event)/sizeof(event[0]), event, sizeof(object_node)/sizeof(object_node[0]), object_node, cmsSample_threadHandler, NULL) == CMS_NOK){
		CMS_LOG(LL_ERROR,  "subscribe object fail.\n");
		goto exit;
	}
	
	while(1){
		sleep(2);
	}

exit:	
	cms_unregisterModule(CMS_SAMPLE); 
	cmsXml_unSubcribeObject(sizeof(event)/sizeof(event[0]), event, sizeof(object_node)/sizeof(object_node[0]), object_node);
	XMLSHMDEINIT(xmlShmCb_g);
	printf("cms sample Exit.\n");
	
	return 0;
}
#endif
